package com.ideal.gui;

import com.ideal.constant.LocalPathConstant;
import com.ideal.service.RefactorMarkdownService;
import com.ideal.service.impl.RefactorMarkdownServiceImpl;
import com.ideal.request.LocalPathRequest;
import org.apache.commons.lang3.StringUtils;

import javax.swing.*;
import java.io.File;

/**
 * 图形界面
 *
 * @author baiwenhuang
 * @date 2023-02-09 16:37
 **/
public class RefactorGui extends JFrame {
    JFrame frame;
    JButton selectButton, refactorButton;
    JTextArea pathTextArea, resultTextArea;
    JPanel panel;
    JLabel label1, label2, label3;
    File file;
    JRadioButton yesRadio, noRadio;


    public static void main(String[] args) {
        new RefactorGui();
    }

    public RefactorGui() {
        // 这是一个窗口类
        frame = new JFrame();
        // 设置窗口位置与大小
        frame.setBounds(100, 100, 680, 400);
        // 用户关闭时会关闭所有窗口
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        // 设置这窗口可见,记得尽量要把这一句放在最后面
        frame.setVisible(Boolean.TRUE);

        // 创建一个面板,并且将这个面板添加到窗口里面去
        panel = new JPanel();
        panel.setLayout(null);
        frame.add(panel);

        label1 = new JLabel("路径");
        label1.setBounds(30, 50, 100, 35);
        panel.add(label1);

        label2 = new JLabel("结果");
        label2.setBounds(30, 220, 100, 35);
        panel.add(label2);

        label3 = new JLabel("包含子层级重构");
        label3.setBounds(545, 135, 100, 35);
        panel.add(label3);

        // 文件路径区域
        pathTextArea = new JTextArea();
        pathTextArea.setBounds(80, 50, 450, 35);
        pathTextArea.setEditable(true);
        panel.add(pathTextArea);

        // 创建一个按钮
        selectButton = new JButton("选择");
        selectButton.setBounds(550, 50, 100, 35);
        selectButton.setVisible(Boolean.TRUE);
        panel.add(selectButton);

        refactorButton = new JButton("执行");
        refactorButton.setBounds(550, 240, 100, 35);
        refactorButton.setVisible(Boolean.TRUE);
        panel.add(refactorButton);

        // 创建单选钮
        yesRadio = new JRadioButton("是");
        yesRadio.setBounds(545, 180, 50, 35);

        noRadio = new JRadioButton("否");
        noRadio.setBounds(595, 180, 50, 35);

        // 创建按钮组
        ButtonGroup group = new ButtonGroup();
        group.add(yesRadio);
        group.add(noRadio);

        panel.add(yesRadio);
        panel.add(noRadio);

        // 设置默认选择
        noRadio.setSelected(true);

        // 结果回显窗口
        resultTextArea = new JTextArea();
        resultTextArea.setBounds(80, 130, 450, 160);
        resultTextArea.setEditable(Boolean.FALSE);
        panel.add(resultTextArea);


        //设置这窗口可见,记得尽量要把这一句放在最后面
        frame.setVisible(true);

        // 选择按钮监听
        selectButton.addActionListener(e -> {
            JFileChooser fileChooser = new JFileChooser();
            fileChooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
            fileChooser.showDialog(new JLabel(), "选择");
            file = fileChooser.getSelectedFile();
            pathTextArea.setText(file.getAbsolutePath());
        });

        // 重构按钮监听
        refactorButton.addActionListener(e -> {
            refactorButton.setEnabled(Boolean.FALSE);
            String resultStr = "";
            if (file.isDirectory()) {
                resultStr = doRefactorLocalPath(file.getAbsolutePath());
            } else if (file.isFile()) {
                resultStr = doRefactorLocalFile(file.getAbsolutePath());
            }
            if (!StringUtils.isEmpty(resultStr)) {
                resultTextArea.setText(resultStr);
                JOptionPane.showMessageDialog(null, "语雀 markdown 文件重构修复成功", "提示", JOptionPane.YES_OPTION);
            }
            refactorButton.setEnabled(Boolean.TRUE);
        });
    }


    public String doRefactorLocalFile(String localFile) {
        RefactorMarkdownService refactorMarkdownService = new RefactorMarkdownServiceImpl();
        LocalPathRequest pathVo = new LocalPathRequest();
        pathVo.setLocalPath(localFile);
        pathVo.setPathType(LocalPathConstant.FILE_PATH);
        return refactorMarkdownService.doRefactorLocalForGui(pathVo);
    }


    public String doRefactorLocalPath(String localPath) {
        RefactorMarkdownService refactorMarkdownService = new RefactorMarkdownServiceImpl();
        LocalPathRequest pathVo = new LocalPathRequest();
        pathVo.setLocalPath(localPath);
        pathVo.setPathType(LocalPathConstant.DIR_PATH);
        if (yesRadio.isSelected()) {
            pathVo.setDirRecursive(1);
        }
        return refactorMarkdownService.doRefactorLocalForGui(pathVo);
    }


}